// 引入数据库
const conn = require('../data/gy_index')
// 显示分类设置页面
module.exports.lyj_knowledge_set = (req, res) => {
    if (!req.session.isgaoyuan) {
        res.redirect('/gy_login');
        return;
    }
    res.render('lyj_knowledge_set');
}
// 显示添加分类页面
module.exports.lyj_add_classify = (req, res) => {
    if (!req.session.isgaoyuan) {
        res.redirect('/gy_login');
        return;
    }
    res.render('lyj_add_classify');
}

module.exports.lyj_add_classify2 = (req, res) => {
    // res.render('lyj_add_classify2');
    // console.log(req.query.id);
    conn.query('select * from lyj_classify where id = ?', req.query.id, (err, result) => {
        if (err) {
            return console.log(err);
        }
        res.render('lyj_add_classify2', result[0]);
    })
}
// 添加数据
module.exports.writeAdd = (req, res) => {
    // console.log(req.query.title);
    conn.query('insert into lyj_classify (name) values(?)', [req.query.title], (err, result) => {
        if (err) {
            return console.log(err);
        }
        if (result) {
            res.json({
                code: '1101',
                message: '保存成功'
            })
        }
    })
}


module.exports.showUser = (req, res) => {
    conn.query('select * from lyj_classify', (err, result) => {
        if (err) {
            console.log(err)
        } else {
            res.json(result)
        }
    })
}

module.exports.delUser = (req, res) => {
    conn.query('delete from lyj_classify where id= ' + req.query.id, (err, result) => {
        if (err) {
            console.log(err)
        } else {
            res.json({
                code: '1201',
                message: '删除成功'
            })
        }
    })
}


module.exports.userUpdate = (req, res) => {
    // console.log(params);
    conn.query('update lyj_classify set name=? where id=?', [req.body.title, req.body.id], (error, result) => {
        if (error) return console.log(error);

        //   console.log(results);
        if (result) {
            res.json({
                code: '1002',
                message: '更新成功'
            })
        }
    })
}



